/**
 * @author 蔡明宗
 * @date 2011-6-12 上午12:02:15
 */

package com.ad.models;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;

import com.ad.system.Common;

public class Weather {

	private static Logger log = Logger.getLogger(Weather.class);

	private String weatherIconUrl;

	private String temp;

	private String date;

	private List<String> weatherList;

	private String item;

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getWeatherIconUrl() {
		return weatherIconUrl;
	}

	public void setWeatherIconUrl(String weatherIconUrl) {
		this.weatherIconUrl = weatherIconUrl;
	}

	public String getTemp() {
		return temp;
	}

	public void setTemp(String temp) {
		this.temp = temp;
	}

	public Weather(String rawHtml) {
		rawHtml = rawHtml.toLowerCase();
		rawHtml = Common.splitMatchFirst("<span id=hf_002>", "</table>",
				rawHtml);
		if (Common.isEmpty(rawHtml)) {
			return;
		}

		// StringBuffer resultBuffer = new StringBuffer();
		int i = 0;
		for (i = 0; i > -5; i--) {
			date = Common.getDateString(i);
			if (rawHtml.contains(date)) {
				break;
			}
		}

		int dayOrNight = getDayOrNight();

		String[] raw = rawHtml.split(date);

		this.weatherList = new ArrayList<String>();
		if (raw.length == 3) {
			weatherList.add(raw[1]);
			raw = raw[2].split("</tr>");
			weatherList.add(raw[0]);
			item = weatherList.get(dayOrNight);
		} else {
			raw = raw[1].split("</tr>");
			weatherList.add(raw[0]);
			item = weatherList.get(0);
		}

	}

	private int getDayOrNight() {
		int hourOfDay = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		int dayOrNight = 0;
		if (hourOfDay >= 0 && hourOfDay <= 18) {
			dayOrNight = 1;
		} else {
			dayOrNight = 0;
		}
		return dayOrNight;
	}

	public List<String> getWeatherList() {
		return weatherList;
	}

	public String getItem() {
		return item;
	}

	public void setItem(String item) {
		this.item = item;
	}

	public String getWeatherHTMLCode() {
		if (Common.isEmpty(item)) {
			return "";
		}
		StringBuffer resultBuffer = new StringBuffer();
		try {
			// 天气图标" width"
			weatherIconUrl = Common.splitMatchFirst("src=", " width", item);
			String info = Common.splitMatch(">", "<", 3, item);
			// 温度
			temp = Common.splitMatch("<td>", "</td>", 2, item).toUpperCase();
			resultBuffer.append(weatherIcon(this.getWeatherIconUrl())).append(
					info).append(" ").append(date).append(" ").append(
					this.getTemp());

		} catch (Exception e) {
			log.info("when extracting weather html code, get exception:"
					+ e.getMessage());
			e.printStackTrace();
		}
		return resultBuffer.toString().trim();
	}

	private String weatherIcon(String url) {
		if (Common.isEmpty(url)) {
			return "";
		}
		String result = "<img style='position:relative;top:6px' src='" + url
				+ "'/>";
		return result;
	}

}
